package com.software.car.service;

import com.software.car.api.ExampleService;
import com.software.car.common.enums.ResultEnum;
import com.software.car.common.exception.MyException;
import com.software.car.common.utils.SnowflakeIdGenerator;
import com.software.car.entity.dto.ExampleDto;
import com.software.car.entity.dto.ExampleValueDto;
import com.software.car.entity.vo.ExampleValueVo;
import com.software.car.mapper.ExampleMapper;
import com.software.car.mapper.ExampleValueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 魔法はまだ解けない
 * @Date 2021/3/21
 */
@Service
public class ExampleServiceImpl implements ExampleService {

    @Autowired
    ExampleMapper exampleMapper;

    @Autowired
    ExampleValueMapper exampleValueMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addExample(ExampleDto exampleDto) {
        //查找是否已经有1型实例
        String example = exampleMapper.isExit(exampleDto.getProjectId(),1);
        if (example!=null){
            throw new MyException(ResultEnum.FORM_HAS_EXIT);
        }
        List<ExampleValueDto> exampleValuelist = exampleDto.getExampleValuelist();
        exampleValuelist.forEach(e->{
            e.setId(SnowflakeIdGenerator.getId());
        });
        String exampleId = SnowflakeIdGenerator.getId();
        exampleDto.setExampleId(exampleId);
        int i = exampleMapper.addExample(exampleDto);
        exampleValueMapper.addExampleValue(exampleValuelist, exampleId);
        return exampleId;
    }

    @Override
    public List<String> getExampleList(String projectId,Integer state) {
        List<String> example1 = exampleMapper.getExample(projectId,state);
        return example1;
    }

    @Override
    public int deleteExampleAll(String projectId) {
        //删除全部实例
        int i = exampleMapper.deleteExample(projectId);
        List<String> example = exampleMapper.getExample(projectId, null);
        if(example==null){
            return 0;
        }
        //删除每个实例的值
        example.forEach(e->{
            int i1 = exampleValueMapper.deleteExampleValue(e);
        });
        return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteExample(String exampleId) {
        int i = exampleMapper.deleteExampleById(exampleId);
        if(i==0){
            throw new MyException(ResultEnum.EXAMPLE_DELETE_ERROR);
        }
        int i1 = exampleValueMapper.deleteExampleValue(exampleId);
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateExample(ExampleDto exampleDto) {
        String example = exampleMapper.isExit(exampleDto.getExampleId(), exampleDto.getType());
        if(example==null){
            throw new MyException(ResultEnum.EXAMPLE_ERROR);
        }
        int i = exampleValueMapper.deleteExampleValue(exampleDto.getExampleId());
        exampleDto.getExampleValuelist().forEach(exampleValueDto -> {
            exampleValueDto.setId(SnowflakeIdGenerator.getId());
        });
        int i1 = exampleValueMapper.addExampleValue(exampleDto.getExampleValuelist(), exampleDto.getExampleId());
        return i1;
    }

    @Override
    public List<ExampleValueVo> getExampleValueDtos(String exampleId) {
        List<ExampleValueVo> exampleValueVo = exampleValueMapper.getExampleValueVo(exampleId);
        return exampleValueVo;
    }
}
